﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Forums.Controllers
{
	using System.Collections.Generic;
	using System.Web.Mvc;
	using Falcon.Core.Controllers;
	using Falcon.Forums.Domain;
	using Falcon.Forums.Domain.Enums;
	using Falcon.Forums.Domain.Services;
	using Falcon.Forums.Domain.Values;

	[Authorize]
	public class SystemForumTopicController : DefaultController
	{
		public SystemForumTopicController(
			IForumService forumService, 
			IForumTopicService forumTopicService)
		{
			this.ForumService = forumService;
			this.ForumTopicService = forumTopicService;
		}

		public IForumService ForumService
		{
			get;
			private set;
		}

		public IForumTopicService ForumTopicService
		{
			get;
			private set;
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Update(ForumTopicUpdate value)
		{
			var forum = this.ForumService.GetById(value.Id);

			if (!forum.CanUpdateAnyTopic)
			{
				return NotAuthorized();
			}

			var topics = new List<ForumTopic>();

			this.ForumTopicService.Transform(value.TopicsToUpdate, topics);

			switch (value.Action)
			{
				case ForumTopicAction.Delete:
					{
						return View(Views.Delete, Masters.System, new ForumTopicDelete
						{
							Topics = topics
						});
					}
				case ForumTopicAction.DeleteSpam:
					{
						return View(ForumsViews.Spam, Masters.System, new ForumTopicDeleteSpam
						{
							Topics = topics
						});
					}
				case ForumTopicAction.Move:
					{
						return View(Views.Move, Masters.System, new ForumTopicMove
						{
							Topics = topics
						});
					}
				case ForumTopicAction.Merge:
					{
						return View(ForumsViews.Merge, Masters.System, new ForumTopicMerge
						{
							Topics = topics
						});
					}
				default:
					{
						this.ForumTopicService.Update(topics, value.Action);
					}
					break;
			}

			return Redirect(value.Redirect);
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Move(ForumTopicMove value)
		{
			var forum = this.ForumService.GetById(value.ForumId);

			if (!forum.CanMoveAnyTopic)
			{
				return NotAuthorized();
			}

			this.ForumTopicService.Move(value);

			return Redirect(value.Redirect);
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Merge(ForumTopicMerge value)
		{
			var forum = this.ForumService.GetById(value.ForumId);

			if (!forum.CanMoveAnyTopic)
			{
				return NotAuthorized();
			}

			this.ForumTopicService.Merge(value);

			return Redirect(value.Redirect);
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Delete(ForumTopicDelete value)
		{
			var forum = this.ForumService.GetById(value.Id);

			if (!forum.CanDeleteAnyTopic)
			{
				return NotAuthorized();
			}

			this.ForumTopicService.Delete(value);

			return Redirect(value.Redirect);
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Spam(ForumTopicDeleteSpam value)
		{
			var forum = this.ForumService.GetById(value.Id);

			if (!forum.CanDeleteAnyTopic)
			{
				return NotAuthorized();
			}

			this.ForumTopicService.Delete(value);

			return Redirect(value.Redirect);
		}
	}
}